package com.tmpt.controller.springmvc.handlers;

import com.tmpt.controller.springmvc.PageUtils;
import com.tmpt.dto.Result;
import com.tmpt.utils.JsonPaginationUtils;
import com.tmpt.utils.StringUtils;
import com.tmpt.utils.WebUtils;
import com.tmptSource.dto.*;
import com.tmptSource.service.intf.IA2appversionService;
import com.tmptSource.service.intf.IOwBaseCommiteeService;
import com.tmptSource.service.intf.IOwBaseRoadService;
import com.tmptSource.service.intf.IOwRegisterDataService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wxy on 2017/10/17.
 */

@Controller //这样标识后HelloWorld成为控制器，在springmvc叫做handler处理器或请求处理器
@RequestMapping("/springmvc/basedata")
public class BaseDataAction {

    @Resource
    private IOwBaseCommiteeService owBaseCommiteeService;
    @Resource
    private IOwBaseRoadService owBaseRoadService;
    @Resource
    private IOwRegisterDataService owRegisterDataService;
    @Resource
    private IA2appversionService a2appversionService;

    /**
     * 添加居委
     * @return
     */
    @RequestMapping(value = "/addBaseCommitee")
    @ResponseBody
    public Result addBaseCommitee(OwBaseCommiteeDto owBaseCommiteeDto, HttpServletRequest request){
        HttpSession session = request.getSession();

        Result<OwBaseCommiteeDto> result = new Result();
        try {
            if(!WebUtils.isLoginSuccess(session)){
                return result.fault("请先登录");
            }

            //插入参数
            String adminMan = WebUtils.getUsername(session);//加入管理员
            String userId = WebUtils.getUserId(session);
            String companyId = WebUtils.getCompanyId(session);
            owBaseCommiteeDto.setCreatemanId(userId);
            owBaseCommiteeDto.setCreateman(adminMan);
            owBaseCommiteeDto.setCompanyId(companyId);
            //插入数据
            //owBaseCommiteeDto.setCommiteeName(this.commiteeName);

            //调用远程服务
            result =  owBaseCommiteeService.addData(owBaseCommiteeDto);

            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.fault("Exception:"+e.getLocalizedMessage());
        }finally {
        }
    }


    /**
     * 查询居委列表
     * @return
     */
    @RequestMapping(value = "/queryBaseCommiteeList")
    @ResponseBody
    public Result queryBaseCommiteeList(HttpServletRequest request){
        HttpSession session = request.getSession();

        Result<OwBaseCommiteeDto> result = new Result();
        try {
            if(!WebUtils.isLoginSuccess(session)){
                return result.fault("请先登录");
            }
            //调用远程服务
            result =  owBaseCommiteeService.selectAll();

            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.fault("Exception:"+e.getLocalizedMessage());
        }finally {
        }
    }



    /**
     * 添加居委管理道路
     * @return
     */
    @RequestMapping(value = "/addBaseRoad")
    @ResponseBody
    public Result addBaseRoad(OwBaseRoadDto owBaseRoadDto, HttpServletRequest request){
        HttpSession session = request.getSession();

        Result<OwBaseRoadDto> result = new Result();
        try {

            if(!WebUtils.isLoginSuccess(session)){
                return result.fault("请先登录");
            }
            //插入参数
            String adminMan = WebUtils.getUsername(session);//加入管理员
            String userId = WebUtils.getUserId(session);
            String companyId = WebUtils.getCompanyId(session);
            owBaseRoadDto.setCreatemanId(userId);
            owBaseRoadDto.setCreateman(adminMan);
            owBaseRoadDto.setCompanyId(companyId);

            //调用远程服务
            result =  owBaseRoadService.addData(owBaseRoadDto);

            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.fault("Exception:"+e.getLocalizedMessage());
        }finally {
        }
    }


    /**
     * 查询居委管理道路列表
     * @return
     */
    @RequestMapping(value = "/queryBaseRoadList")
    @ResponseBody
    public Result queryBaseRoadList(OwBaseRoadDto owBaseRoadDto,HttpServletRequest request){
        HttpSession session = request.getSession();

        Result<OwBaseRoadDto> result = new Result();
        try {
            if(!WebUtils.isLoginSuccess(session)){
                return result.fault("请先登录");
            }
            //调用远程服务
            result =  owBaseRoadService.pagination(owBaseRoadDto, null);

            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.fault("Exception:"+e.getLocalizedMessage());
        }finally {
        }
    }

    /**
     * 查询居委管理道路列表Tree
     * @return
     */
    @RequestMapping(value = "/queryCommiteeTree")
    @ResponseBody
    public Result queryCommiteeTree(OwBaseRoadDto owBaseRoadDto,HttpServletRequest request){
        HttpSession session = request.getSession();

        Result<OwBaseCommiteeDto> result = new Result();
        try {
            if(!WebUtils.isLoginSuccess(session)){
                return result.fault("请先登录");
            }
            //调用远程服务
            result =  owBaseCommiteeService.queryCommiteeTree();

            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.fault("Exception:"+e.getLocalizedMessage());
        }finally {
        }
    }

    /**
     * 数据字典
     * @return
     */
    @RequestMapping(value = "/queryBaseDataTree")
    @ResponseBody
    public Result queryBaseDataTree(HttpServletRequest request){
        HttpSession session = request.getSession();

        Result result = new Result();
        try {
            if(!WebUtils.isLoginSuccess(session)){
                return result.fault("请先登录");
            }
            //调用远程服务
            result =  owBaseCommiteeService.queryBaseDataTree();

            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.fault("Exception:"+e.getLocalizedMessage());
        }finally {
        }
    }


    /**
     * 信息登记
     * @return
     */
    @RequestMapping(value = "/addRegisterData")
    @ResponseBody
    public Result addRegisterData(OwRegisterDataDto owRegisterDataDto, HttpServletRequest request){
        HttpSession session = request.getSession();

        Result<OwRegisterDataDto> result = new Result();
        try {

            if(!WebUtils.isLoginSuccess(session)){
                return result.fault("请先登录");
            }
            //插入参数
            String adminMan = WebUtils.getUsername(session);//加入管理员
            String userId = WebUtils.getUserId(session);
            String companyId = WebUtils.getCompanyId(session);
            owRegisterDataDto.setCreatemanId(userId);
            owRegisterDataDto.setCreateman(adminMan);
            owRegisterDataDto.setCompanyId(companyId);

            //调用远程服务
            result =  owRegisterDataService.addEditData(owRegisterDataDto);

            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.fault("Exception:"+e.getLocalizedMessage());
        }finally {
        }
    }


    /**
     * 查询登记信息
     * @return
     */
    @RequestMapping(value = "/queryRegisterDataList")
    @ResponseBody
    public Result queryRegisterDataList(OwRegisterDataDto owRegisterDataDto,PageDto pageDto,HttpServletRequest request){
        HttpSession session = request.getSession();

        Result<OwRegisterDataDto> result = new Result();
        try {
            if(!WebUtils.isLoginSuccess(session)){
                return result.fault("请先登录");
            }
            //分页/排序查询条件
            //默认主键desc
            pageDto = PageUtils.resetPageDto(pageDto);

            if(!StringUtils.isNull(owRegisterDataDto.getPosLat()) && !StringUtils.isNull(owRegisterDataDto.getPosLng())){
                //经纬度查询附近
                result = owRegisterDataService.selectByPaginationByLatLng(owRegisterDataDto,pageDto);
            }
            else {
                //普通查询条件
                if(!StringUtils.isNull(owRegisterDataDto.getPosDesc())) {
                    owRegisterDataDto.setPosDesc("%" + owRegisterDataDto.getPosDesc() + "%");
                }
                result = owRegisterDataService.pagination(owRegisterDataDto, pageDto);
            }

            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.fault("Exception:"+e.getLocalizedMessage());
        }finally {
        }
    }





    /**
     * APP升级版本号
     * @return
     */
    @RequestMapping(value = "/queryAppVersion")
    @ResponseBody
    public Result<A2appversionDto> queryAppVersion(A2appversionDto a2appversionDto, HttpServletRequest request){
        HttpSession session = request.getSession();

        Result<A2appversionDto> result = new Result();
        try {
            PageDto pageDto = PageUtils.resetPageDto(null);
            //调用远程服务
            result =  a2appversionService.pagination(a2appversionDto, pageDto);
            List<A2appversionDto> list = (List<A2appversionDto>)result.getResultMap().get("rows");
            if(list.size() > 0){
                result.setResultObj(list.get(0));
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.fault("Exception:"+e.getLocalizedMessage());
        }finally {
        }


    }





}